package project.dwd.wit.ie.tripfinancemanager;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;


public class NewTripActivity extends ActionBarActivity {

    private EditText tripName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_trip);

        tripName = (EditText) findViewById(R.id.new_trip_edit_text);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_new_trip, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public EditText getTripName() {
        return tripName;
    }

    public void saveNewTrip(View view) {
        String _tripName = getTripName().getText().toString();
        String filename = _tripName.replace(" ", "_") + ".dat";

        File file = new File(getCacheDir() + "/" + filename.toLowerCase());
        Writer out = null;
        if (file.exists()) {
            file.delete();
        }
        try {
            out = new BufferedWriter(new FileWriter(file));
            out.write(_tripName + "\n\n");
            out.flush();
            out.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        Log.e("saveNewTrip", "Saving a New Trip in a file");
        Log.e("FileName >> ", getCacheDir() + "/" + filename);
        toastConfirmationNewTrip();
    }

    public void toastConfirmationNewTrip() {
        Context context = getApplicationContext();
        CharSequence text = "A new trip has been added successfully.";
        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
        finish();
    }

    /* Checks if external storage is available for read and write */
    public boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }

    /* Checks if external storage is available to at least read */
    public boolean isExternalStorageReadable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state) ||
                Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return true;
        }
        return false;
    }
}

//    public void readNewTrip() {
//        FileInputStream fs;
//        try {
//            File temf=new File(getCacheDir()+"/data/mytext.txt");
//            if (temf.exists()) {
//                fs = new FileInputStream(temf);
//                DataInputStream dis = new DataInputStream(fs);
//                BufferedReader br = new BufferedReader(new InputStreamReader(
//                        dis));
//                String str=br.readLine();
//                while(str!=null)
//                {
//                    if(str=="text you want to compare"){
//                        break;
//                    }else{
//                        str=br.readLine();
//                    }
//                }
//            }
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }


//        new AlertDialog.Builder(this)
//                .setTitle("A new trip has been added.")
//                .setMessage("Would you like to start adding costs?")
//                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int which) {
//                        // continue with delete
//                    }
//                })
//                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int which) {
//
//                        Context context = getApplicationContext();
//                        CharSequence text = "new trip added successfully.";
//                        int duration = Toast.LENGTH_LONG;
//
//                        Toast toast = Toast.makeText(context, text, duration);
//                        toast.show();
//                        finish();
//                    }
//                })
//                .setIcon(android.R.drawable.ic_dialog_alert)
//                .show();
